<template>
    <div>
        <!--面包屑-->
        <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
            <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>员工管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!--用户列表卡片-->
        <el-card class="box-card">
            <el-form :inline="true" :model="departments" class="demo-form-inline">
                <el-form-item label="部门编号" label-width="70px">
                    <el-input clearable v-model="departments.dNum" placeholder="请输入部门编号"></el-input>
                </el-form-item>
                <el-form-item label="部门名称" label-width="70px">
                    <el-input clearable v-model="departments.dName" placeholder="请输入部门名称"></el-input>
                </el-form-item>
                <el-form-item label="经理名称" label-width="70px">
                    <el-input clearable v-model="departments.cName" placeholder="请输入部门经理名称"></el-input>
                </el-form-item>

                <el-form-item style="margin-left: 10px">
                    <el-button icon="el-icon-refresh" @click="resetDepartmentVo">重置</el-button>
                    <el-button type="primary" icon="el-icon-search" @click="getDepartmentList">查询</el-button>
                    <span v-if="status">
                        <el-button type="success" icon="el-icon-plus" @click="showAdd">添加</el-button>
                    </span>
                    <el-button type="warning" icon="el-icon-download">导出</el-button>
                </el-form-item>
            </el-form>
            <!--表格内容显示区域-->
            <el-table
                    :data="departmentList"
                    border
                    max-height="600"
                    style="width: 100%;">
                <el-table-column
                        prop="dNum"
                        label="部门编号"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="dName"
                        label="部门名称"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="cName"
                        label="部门经理名称"
                        width="200">
                </el-table-column>

                <el-table-column
                        width="200"
                        label="操作">
                    <template slot-scope="scope">
                      <span v-if="status">
                        <el-button type="primary" size="mini" icon="el-icon-edit"
                                   @click.native.prevent="showUpdate(scope.row.dNum)"></el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete"
                                   @click.native.prevent="deleteDepartment(scope.row.dNum)"></el-button>
                        <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
                      </span>
                      <span v-if="!status">
                        无权限
                      </span>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    style="padding-top: 15px"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="current"
                    :page-sizes="[6, 10, 20, 30]"
                    :page-size="size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>

            <DepartmentAdd :addVisible="addVisible" @changeShowAdd="showAdds" ref="addRef"></DepartmentAdd>
            <DepartmentUpdate :dNums="dNums" :updateVisible="updateVisible" @changeShowUpdate="showUpdates"
                              ref="UpdateRef"></DepartmentUpdate>
        </el-card>
    </div>
</template>

<script>


    import DepartmentAdd from "./DepartmentAdd";
    import DepartmentUpdate from "./DepartmentUpdate";

    export default {
        name: 'Departments',
        data() {
            return {
                departments: {
                    dNum: '',
                    dName: '',
                    cName: '',
                },
                // 控制新增编辑弹窗的显示与隐藏
                addVisible: false,
                updateVisible: false,
                dNums: '',
                //用户集合
                departmentList: [],
                //每页显示的条数
                size: 6,
                //总条数
                total: 100,
                //当前页码
                current: 1,
                status: true
            }
        },
        components: {
            DepartmentAdd,
            DepartmentUpdate
        },
        created() {
            //创建组件的时候调用查询所有用户的方法
            this.getDepartmentList()
          this.getStatus()
        },
        methods: {
            onSubmit() {
                console.log('submit!');
            },
            //当每页条数改变的时候
            handleSizeChange(val) {
                //将val赋值给size
                this.size = val;
                //重新去后台查询数据
                this.getDepartmentList();
            },
            //当前页码改变的时候
            handleCurrentChange(val) {
                this.current = val;
                this.getDepartmentList();
            },
            async getDepartmentList() {
                this.$axios.post("/sys/department/search/" + this.current + "/" + this.size, this.departments).then((response) => {
                    console.log(response)
                    this.departmentList = response.data.data.list;
                    this.total = response.data.data.total;
                })
            },
            getStatus() {
                this.$axios.delete("/sys/salary/" + 2010010).then((response) => {
                    console.log(response.data.code);
                    if (response.data.code != 20000) {
                        this.status = false
                    }
                });
            }
            ,
            deleteDepartment(id) {
                this.$axios.delete("/sys/department/" + id).then((response) => {
                });
                location.reload();
            }
            ,
            //重置表单
            resetDepartmentVo() {
                this.departments.dNum = '',
                    this.departments.dName = '',
                    this.departments.cNum = '',

                    this.getDepartmentList()
            },
            // 按钮点击事件 显示新增编辑弹窗组件
            showAdd() {
                this.addVisible = true
            },
            showUpdate(id) {
                this.updateVisible = true;
                this.dNums = id;
                console.log("id=" + id);
            },
            // 监听 子组件弹窗关闭后触发，有子组件调用
            showAdds(data) {
                if (data === 'false') {
                    this.addVisible = false
                } else {
                    this.addVisible = true
                }
            },
            showUpdates(data) {
                if (data === 'false') {
                    this.updateVisible = false
                } else {
                    this.updateVisible = true
                }
            }
        }
    }
</script>

<style scoped>

</style>
